package com.lkm.app_ui;


import com.lkm.app_ui.jz.ui.fragment.ShopScreenFragment;
import com.lkm.app_ui.m.fun.DelegatePayTask;
import com.lkm.app_ui.ui.BarcodeResultFragment;
import com.lkm.app_ui.ui.CountNumFragment;
import com.lkm.app_ui.ui.FindBarcodeResultFragment;
import com.lkm.app_ui.ui.GoodsButtonFragment;
import com.lkm.app_ui.ui.InfoFragment;
import com.lkm.app_ui.ui.InfoQrFragment;
import com.lkm.app_ui.jz.ui.fragment.PayFragment01;
import com.lkm.app_ui.ui.PayFragment;
import com.lkm.app_ui.ui.PayOutlineFragment;
import com.lkm.app_ui.ui.PaySuccessTipsFragment;
import com.lkm.app_ui.ui.SaleFunFragment;
import com.lkm.app_ui.ui.VipOrderFixFragment;
import com.lkm.app_ui.ui.WifiFragment;
import com.lkm.app_ui.ui.fj.FjOrderFragment;
import com.lkm.app_ui.ui.fj.FjWaitOrderFragment;
import com.lkm.app_ui.ui.fj.PayFjFragment;
import com.lkm.app_ui.ui.sale.SaleUIFragment;
import com.lkm.comlib.ui.BaseFragmentActivity;
import com.lkm.frame.P;

import java.util.WeakHashMap;

import androidx.fragment.app.Fragment;


public class RouterUINavigation {

    private static RouterUINavigation instance = new RouterUINavigation();

    public static RouterUINavigation getInstance() {
        return instance;
    }

    public void showFragment(BaseFragmentActivity activity, Fragment payFragment) {
        showFragment(activity, payFragment, true);
    }

    public void showFragment(BaseFragmentActivity activity, Fragment payFragment, boolean likeDialog) {
        try {
            DialogWrapFragment f = DialogWrapFragment.getInstance(payFragment, likeDialog);

            f.show(activity.getSupportFragmentManager(), payFragment.getClass().getName());
            showFragmentMap.put(payFragment, f);
        } catch (Exception e) {
        }
    }


    WeakHashMap<Fragment, DialogWrapFragment> showFragmentMap = new WeakHashMap<>();

    public void showSaleFun(BaseFragmentActivity activity, Fragment targetFragment, int requestCode) {
        SaleFunFragment saleFunFragment = SaleFunFragment.getInstance(targetFragment, requestCode);
        showFragment(activity, saleFunFragment);
    }


    public void goSaleFragment(BaseFragmentActivity activity) {
        try {

            SaleUIFragment saleFunFragment = SaleUIFragment.getInstance(false);
            activity.getSupportFragmentManager().beginTransaction().replace(com.lkm.comlibxx.R.id.content, saleFunFragment).commitAllowingStateLoss();
        } catch (Exception e) {
//            java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
//            at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2080)
//            at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2106)
//            at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
//            at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:637)
        }
    }


    public void showFjOrder(BaseFragmentActivity activity, boolean exitFj_ls) {
        FjOrderFragment fjOrderFragment = FjOrderFragment.getInstance(exitFj_ls);
        dismissAll();
        showFragment(activity, fjOrderFragment);
    }

    public void showFjWaitOrder(BaseFragmentActivity activity) {
        FjWaitOrderFragment fjOrderFragment = new FjWaitOrderFragment();
        dismissAll();
        showFragment(activity, fjOrderFragment);
    }

    public void showPayFjOrder(BaseFragmentActivity activity) {
        PayFjFragment fjOrderFragment = new PayFjFragment();
        dismissAll();
        showFragment(activity, fjOrderFragment);
    }

    public void showShopScreen(BaseFragmentActivity activity) {
        ShopScreenFragment shopScreenFragment = ShopScreenFragment.getInstance();
        dismissAll();
        showFragment(activity, shopScreenFragment);
    }

    public void showPay01(BaseFragmentActivity activity, @DelegatePayTask.PayType int payType, String url, String order_num) {
        //P.p("Bill-> RouterUINavigation ->showPay->payType:" + payType);
        PayFragment01 payFragment = PayFragment01.getInstance(payType, url, order_num);
        dismissAll();
        showFragment(activity, payFragment);
    }

    public void showPay(BaseFragmentActivity activity, @DelegatePayTask.PayType int payType) {
        //P.p("Bill-> RouterUINavigation ->showPay->payType:" + payType);
        PayFragment payFragment = PayFragment.getInstance(payType);
        dismissAll();
        showFragment(activity, payFragment);
    }


    public void showPayOutline(BaseFragmentActivity activity) {
        //P.p("Bill-> RouterUINavigation ->showPayOutline");
        PayOutlineFragment payFragment = PayOutlineFragment.getInstance();
        showFragment(activity, payFragment);
    }

    public void showPaySuccess(BaseFragmentActivity activity, String printTable, String orderurl) {
        PaySuccessTipsFragment payFragment = PaySuccessTipsFragment.getInstance(printTable, orderurl);
        dismissAll();
        showFragment(activity, payFragment);
    }

    public void showVipOrderFix(BaseFragmentActivity activity) {
        VipOrderFixFragment payFragment = VipOrderFixFragment.getInstance();
        dismissAll();
        showFragment(activity, payFragment);
    }


    public void showWifi(BaseFragmentActivity activity, boolean isAutoGoScan) {
        WifiFragment payFragment = WifiFragment.getInstance(isAutoGoScan, "");
        showFragment(activity, payFragment);
    }

    public void execCmd(BaseFragmentActivity activity, String cmd) {
        dismissAll();
        WifiFragment payFragment = WifiFragment.getInstance(false, cmd);
        showFragment(activity, payFragment);
    }

    public void showInfo(BaseFragmentActivity activity) {
        InfoFragment payFragment = InfoFragment.getInstance();
        showFragment(activity, payFragment);
    }

    public MBaseFragment showInfoQR(BaseFragmentActivity activity) {
        InfoQrFragment payFragment = InfoQrFragment.getInstance();
        showFragment(activity, payFragment);
        return payFragment;
    }

    public void showGoodsButton(BaseFragmentActivity activity) {
        GoodsButtonFragment payFragment = GoodsButtonFragment.getInstance();
        showFragment(activity, payFragment);
    }


    public MBaseFragment showBarcodeResult() {
        BaseFragmentActivity activity = BaseFragmentActivity.getLastActivity();
        if (activity == null) return null;
        BarcodeResultFragment payFragment = BarcodeResultFragment.getInstance();
        showFragment(activity, payFragment);
        return payFragment;
    }

    public MBaseFragment showCountNum(boolean isBarcodeResult) {
        BaseFragmentActivity activity = BaseFragmentActivity.getLastActivity();
        if (activity == null) return null;
        CountNumFragment payFragment = CountNumFragment.getInstance(isBarcodeResult);
        showFragment(activity, payFragment);
        return payFragment;
    }

    public MBaseFragment showFindBarcodeResult() {
        BaseFragmentActivity activity = BaseFragmentActivity.getLastActivity();
        if (activity == null) return null;
        FindBarcodeResultFragment payFragment = FindBarcodeResultFragment.getInstance();
        showFragment(activity, payFragment);
        return payFragment;
    }

    public void dismissAll() {
        for (DialogWrapFragment dialogWrapFragment :
                showFragmentMap.values()) {
            if (dialogWrapFragment != null) {
                try {
                    dialogWrapFragment.dismissAllowingStateLoss();
                } catch (Exception e) {
                }
            }
        }
        showFragmentMap.clear();

    }

    public void dismiss(Fragment fragment) {
        if (fragment == null) return;
        DialogWrapFragment dialogWrapFragment = showFragmentMap.remove(fragment);
        if (dialogWrapFragment != null) {
            try {
                dialogWrapFragment.dismissAllowingStateLoss();
            } catch (Exception e) {
            }
        }
    }

    public boolean containsKey(Fragment fragment) {
        if (fragment == null) return false;
        return showFragmentMap.containsKey(fragment);
    }

    public boolean isFragmentMapEmpty() {
        return showFragmentMap.isEmpty();
    }
}
